import React, { Component } from "react";
import { Form, Input, InputNumber, Select, Space, Button, Radio } from "antd";
import GoBack from '@/components/TheGoBack';
import {
    apiPetSceneInfoAddOrEdit,
    apiGetPetSceneInfoDetail,
    apiGetPetSceneClassifyList
} from '@/request/sceneTool'

const { Option } = Select
const { TextArea } = Input;
class Add extends Component {
    constructor(props) {
        super(props);
        this.state = {
            btnLoading: false,
            title: '',
            options: []
        }
    }
    componentDidMount() {
        if(this.props.match.params.id === 'null'){
            this.setState({
                title: "新建药物"
            })
        }
        else {
            this.setState({
                title: "编辑药物"
            })
            this.detail(this.props.match.params.id)
        }
        this.getList()
    }
    detail = async (id)=>{
        await apiGetPetSceneInfoDetail({sceneId:id}).then(res=>{
            if (res.code === 0) {
                this.formRef.setFieldsValue(res.data)
            }
        })
    }
    getList= async()=>{
        await apiGetPetSceneClassifyList({ platformSetUseValue: 'DRUG' }).then(res=>{
            if (res.code === 0){
                this.setState({
                    options: res.data
                })
            }
        })
    }
    onFinish = async (values)=>{
        this.setState({btnLoading: true})
        if (this.props.match.params.id !== 'null'){
            values.sceneId = this.props.match.params.id
        }
        values.platformSetUseValue = 'DRUG'
        await apiPetSceneInfoAddOrEdit(values).then(res=>{
            if (res.code === 0) {
                React.$message({
                    type: 'success',
                    content: '操作成功'
                })
                this.props.history.goBack()
            } else {
                this.setState({btnLoading: false})
            }
        })
    }
    render() {
        const { title,btnLoading,options } = this.state

        return (
            <div className="p-layout-box">
                <GoBack history={this.props.history} title={title} />
                <div className="p-layout-content">
                    <Form
                        labelCol={{ span: 3 }}
                        wrapperCol={{ span: 8 }}
                        onFinish={this.onFinish}
                        ref={ref => this.formRef = ref}
                    >
                        <Form.Item rules={[{ required: true, message: '请选择' }]} label="分类" name="sceneClassifyId">
                            <Select options={options} fieldNames={{label: "sceneClassifyName", value: "sceneClassifyId"}}>
                            </Select>
                        </Form.Item>
                        <Form.Item rules={[{required: true}]} label="药品名称" name="sceneName">
                            <Input maxLength={10} placeholder={"最多可填写10个字"}/>
                        </Form.Item>
                        <Form.Item rules={[{required: true}]} label="用药说明" name="sceneText">
                            <TextArea rows={4} maxLength={300} showCount/>
                        </Form.Item>
                        <Form.Item rules={[{required: true, message: '请输入正确的排序数字。不校验排序唯一性' }]} label="排序" name="sorted">
                            <InputNumber min={1} precision={0}/>
                        </Form.Item>
                        <Form.Item
                            wrapperCol={{
                                offset: 3,
                                span: 10,
                            }}
                        >
                            <Space>
                                <Button onClick={() => this.props.history.goBack()}>取消</Button>
                                <Button loading={btnLoading} htmlType="submit" type="primary">保存</Button>
                            </Space>
                        </Form.Item>
                    </Form>
                </div>
            </div>
        );
    }
}

export default Add;
